/*
 * song.c
 * Project 4
 * Nicole Ang (nang1@asu.edu)
 * Bijan Fakhri (bfakhri@asu.edu)
 * CSE 325: Embedded Microprocessor Systems
 * Spring 2013
 */

#include "song.h"

song playThis;
int play;

void song_init(){
	play = 1;//indicate whether a note should be played or not
	playThis.position = 0;
	
	playThis.notes[0].pitch = A3;
	playThis.notes[0].type = sixteenth;
	
	playThis.notes[1].pitch = C4;
	playThis.notes[1].type = sixteenth;
	
	playThis.notes[2].pitch = D4;
	playThis.notes[2].type = eigth_dot;
	
	playThis.notes[3].pitch = D4;
	playThis.notes[3].type = sixteenth;
	
	playThis.notes[4].pitch = D4;
	playThis.notes[4].type = eigth;
	
	playThis.notes[5].pitch = D4;
	playThis.notes[5].type = sixteenth;
	
	playThis.notes[6].pitch = E4;
	playThis.notes[6].type = sixteenth;
	
	playThis.notes[7].pitch = F4;
	playThis.notes[7].type = eigth_dot;
	
	playThis.notes[8].pitch = F4;
	playThis.notes[8].type = sixteenth;
	
	playThis.notes[9].pitch = F4;
	playThis.notes[9].type = eigth;
	
	playThis.notes[10].pitch = F4;
	playThis.notes[10].type = sixteenth;
	
	playThis.notes[11].pitch = G4;
	playThis.notes[11].type = sixteenth;
	
	playThis.notes[12].pitch = E4;
	playThis.notes[12].type = eigth_dot;
	
	playThis.notes[13].pitch = E4;
	playThis.notes[13].type = sixteenth;
	
	playThis.notes[14].pitch = E4;
	playThis.notes[14].type = eigth;
	
	playThis.notes[15].pitch = D4;
	playThis.notes[15].type = sixteenth;
	
	playThis.notes[16].pitch = C4;
	playThis.notes[16].type = sixteenth;
	
	playThis.notes[17].pitch = C4;
	playThis.notes[17].type = sixteenth;
	
	playThis.notes[18].pitch = D4;
	playThis.notes[18].type = sixteenth;
	
	playThis.notes[19].pitch = pause;
	playThis.notes[19].type = sixteenth;
	
	playThis.notes[20].pitch = A3;
	playThis.notes[20].type = sixteenth;
	
	playThis.notes[21].pitch = C4;
	playThis.notes[21].type = sixteenth;
	
	playThis.notes[22].pitch = D4;
	playThis.notes[22].type = eigth_dot;
	
	playThis.notes[23].pitch = D4;
	playThis.notes[23].type = sixteenth;
	
	playThis.notes[24].pitch = D4;
	playThis.notes[24].type = eigth;
	
	playThis.notes[25].pitch = D4;
	playThis.notes[25].type = sixteenth;
	
	playThis.notes[26].pitch = E4;
	playThis.notes[26].type = sixteenth;
	
	playThis.notes[27].pitch = F4;
	playThis.notes[27].type = eigth_dot;
	
	playThis.notes[28].pitch = F4;
	playThis.notes[28].type = sixteenth;
	
	playThis.notes[29].pitch = F4;
	playThis.notes[29].type = eigth;
	
	playThis.notes[30].pitch = F4;
	playThis.notes[30].type = sixteenth;
	
	playThis.notes[31].pitch = G4;
	playThis.notes[31].type = sixteenth;
	
	playThis.notes[32].pitch = E4;
	playThis.notes[32].type = eigth_dot;
	
	playThis.notes[33].pitch = E4;
	playThis.notes[33].type = eigth;
	
	playThis.notes[34].pitch = E4;
	playThis.notes[34].type = eigth;
	
	playThis.notes[35].pitch = D4;
	playThis.notes[35].type = sixteenth;
	
	playThis.notes[36].pitch = C4;
	playThis.notes[36].type = sixteenth;
	
	playThis.notes[37].pitch = D4;
	playThis.notes[37].type = quarter;
	
	playThis.notes[38].pitch = pause;
	playThis.notes[38].type = quarter;
}

void playNextNote(){
	if(play){
		int temp = playThis.position;
		Note nextNote = playThis.notes[temp];
		set_frequency(nextNote.pitch);
		playThis.position = temp + 1;
		//need to change pit to last for note duration
		if(playThis.position > 38)
			playThis.position = 0;
		
		play = 0;
	} else{//should pause between notes
		set_frequency(pause);
		play = 1;
	}
}